package cn.elead.chaos.remote.web.remote.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.elead.chaos.core.constant.UserConstants;
import cn.elead.chaos.core.enums.BusinessType;
import cn.elead.chaos.excel.ExcelUtil;
import cn.elead.chaos.framework.monitor.operlog.Log;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.elead.chaos.framework.web.global.response.UnifiedReturn;
import cn.elead.chaos.mybatils.web.controller.BaseController;
import cn.elead.chaos.mybatils.web.page.TableDataInfo;
import cn.elead.chaos.sso.core.util.SsoUtils;
import cn.elead.chaos.remote.web.remote.domain.SysPost;
import cn.elead.chaos.remote.web.remote.service.ISysPostService;

/**
 * 岗位信息操作处理
 * 
 * @author luopeng
 */
@UnifiedReturn
@RestController
@RequestMapping("/system/post")
public class RemotePostController extends BaseController {
	@Autowired
	private ISysPostService postService;

	/**
	 * 获取岗位列表
	 */
	@GetMapping("/page")
	public TableDataInfo<SysPost> page(SysPost post) {
		startPage();
		List<SysPost> list = postService.selectPostList(post);
		return getDataTable(list);
	}

	@GetMapping("/export")
	@Log(title = "岗位管理", businessType = BusinessType.EXPORT)
	public String export(SysPost post) {
		List<SysPost> list = postService.selectPostList(post);
		ExcelUtil<SysPost> util = new ExcelUtil<SysPost>(SysPost.class);
		return util.exportExcel(list, "岗位数据");
	}

	/**
	 * 根据岗位编号获取详细信息
	 */
	@GetMapping(value = "/{postId}")
	public SysPost getInfo(@PathVariable Long postId) {
		return postService.selectPostById(postId);
	}

	/**
	 * 新增岗位
	 */
	@PostMapping
	@Log(title = "岗位管理", businessType = BusinessType.INSERT)
	public int add(@Validated @RequestBody SysPost post) {
		if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增岗位'" + post.getPostName() + "'失败，岗位名称已存在"));
		} else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("新增岗位'" + post.getPostName() + "'失败，岗位编码已存在"));
		}
		post.setCreateBy(SsoUtils.getUserName());
		return postService.insertPost(post);
	}

	/**
	 * 修改岗位
	 */
	@PutMapping
	// @RequiresPermissions("system:post:edit")
	@Log(title = "岗位管理", businessType = BusinessType.UPDATE)
	public int edit(@Validated @RequestBody SysPost post) {
		if (UserConstants.NOT_UNIQUE.equals(postService.checkPostNameUnique(post))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在"));
		} else if (UserConstants.NOT_UNIQUE.equals(postService.checkPostCodeUnique(post))) {
			ApiAssert.failure(ErrorCodeEnum.BAD_REQUEST.convert("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在"));
		}
		post.setUpdateBy(SsoUtils.getUserName());
		return postService.updatePost(post);
	}

	/**
	 * 删除岗位
	 */
	@DeleteMapping("/{postIds}")
	// @RequiresPermissions("system:post:remove")
	@Log(title = "岗位管理", businessType = BusinessType.DELETE)
	public int remove(@PathVariable Long[] postIds) {
		return postService.deletePostByIds(postIds);
	}

	/**
	 * 获取岗位选择框列表
	 */
	@GetMapping("/optionselect")
	public List<SysPost> optionselect() {
		List<SysPost> posts = postService.selectPostAll();
		return posts;
	}
}
